package springboot.houbank.com.AsyncCall;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.Future;

/**
 * Created by zhaoqiguo on 2016/9/8.
 */

@Component
public class TaskAsync {
    public static Random random =new Random();
    @Async
    public void doTaskOne()throws Exception {
        System.out.println("开始做任务一");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务一，耗时：" + (end - start) + "毫秒");

    }
    @Async
    public void doTaskTwo() throws  Exception{
        System.out.println("开始任务二");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务二，耗时："+(end - start)+"毫秒");
    }
    @Async
    public void doTaskThree() throws  Exception{
        System.out.println("开始任务三");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务三，耗时："+(end - start)+"毫秒");
    }

    @Async
    public Future<String> doTaskFour()throws Exception{
        System.out.println("开始做任务四");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("完成任务四，耗时：" + (end - start) + "毫秒");
        return new AsyncResult<>("任务四完成");
    }
}
